<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
use \Common\Model\Address;
//前台查看地址控制器
class FillInfoController extends Controller{
    
    public function index(){
    	if(isset($_SESSION['user'])){
			$user = $_SESSION['user'];
		}	
		View::with('user',$user);
    	//获取当前用户
		$uid = $_SESSION['user']['uid'];	
    	//获取用户收货地址
		$addressModel = new Address;
		$addressData = $addressModel
						->where("user_uid",$uid)
						->orderBy('de_address','DESC')->get();
		//把user_address 表的address值改成数组形式，用于传递给js
		foreach($addressData as $k=>$v){
			//将字符串转成数组形式
			$addressData[$k]['address'] = explode('|', $v['address']);
		}	
		View::with('addressData',$addressData);
		//判断用户是否直接购买的商品(是)
		if(isset($_SESSION['shopbuy'])){
			/*直接购买==>提交订单*/
			//获取商品数据
	    	$goodsCarData = $_SESSION['shopbuy']; 
			View::with('goodsCarData',$goodsCarData);
			//获得商品总价格
			$goodsCarPrice = $_SESSION['shopbuy']['goods']['total'];		
			//算上运费总价格(默认为8元)
			$expressPay = 8;		
			$goodsPrice = $goodsCarPrice + $expressPay;
			//显示价格
			$goodsPriceTotal = number_format($goodsPrice,2);
			//隐藏价格(存数据库)
			$goodsPriceTotalHid = $goodsPrice;
			//利用千位分组来格式化总价
			$goodsCarPrice = number_format($goodsCarPrice,2);				
			View::with('goodsCarPrice',$goodsCarPrice);		
			View::with('goodsPriceTotal',$goodsPriceTotal);	
			View::with('goodsPriceTotalHid',$goodsPriceTotalHid);	
		}else{
			/*购物车==>提交订单*/
			//获取购物车商品数据
	    	$goodsCarData = Cart::getGoods();
			View::with('goodsCarData',$goodsCarData);
			//获得商品总价格
			$goodsCarPrice = Cart::getTotalPrice();		
			//算上运费总价格(默认为8元)
			$expressPay = 8;		
			$goodsPrice = $goodsCarPrice + $expressPay;
			//显示价格
			$goodsPriceTotal = number_format($goodsPrice,2);
			//隐藏价格(存数据库)
			$goodsPriceTotalHid = $goodsPrice;
			//利用千位分组来格式化总价
			$goodsCarPrice = number_format($goodsCarPrice,2);				
			View::with('goodsCarPrice',$goodsCarPrice);		
			View::with('goodsPriceTotal',$goodsPriceTotal);	
			View::with('goodsPriceTotalHid',$goodsPriceTotalHid);	
								
		}			 			

    	View::make();
    }
	
	//获取异步数据(普通)
	public function getExpress_faa(){
		$expressPay =  q('post.fraa');
		//利用千位分组来格式化邮费
		$expressPay = number_format($expressPay,2);
		if(isset($_SESSION['shopbuy'])){
			/*直接购买==>提交订单*/
			//获得商品总价格
			$goodsCarPrice = $_SESSION['shopbuy']['goods']['total'];	
			
		}else{
			//获得商品总价格
			$goodsCarPrice = Cart::getTotalPrice();	
		}		
		//算上运费总价格
		$goodsPrice = $goodsCarPrice + $expressPay;
		$goodsCarPrice = number_format($goodsPrice,2);
		$goodsPriceTotalHid = $goodsPrice;
		$data = [
			'expressPay'   => $expressPay,
			'goodsCarPrice'=> $goodsCarPrice,
			'goodsPriceTotalHid' => $goodsPrice,
		];	
		echo json_encode($data);exit;		
	}
	
	//获取异步数据(顺丰)
	public function getExpress_fee(){
		$expressPay =  q('post.free');
		//利用千位分组来格式化邮费
		$expressPay = number_format($expressPay,2);	
		if(isset($_SESSION['shopbuy'])){
			/*直接购买==>提交订单*/
			//获得商品总价格
			$goodsCarPrice = $_SESSION['shopbuy']['goods']['total'];	
			
		}else{
			//获得商品总价格
			$goodsCarPrice = Cart::getTotalPrice();	
		}
		//算上运费总价格
		$goodsPrice = $goodsCarPrice + $expressPay;
		$goodsCarPrice = number_format($goodsPrice,2);
		$goodsPriceTotalHid = $goodsPrice;
		$data = [
			'expressPay'   => $expressPay,
			'goodsCarPrice'=> $goodsCarPrice,
			'goodsPriceTotalHid' => $goodsPrice,
		];	
		echo json_encode($data);exit;		
	}
}
